package maitaole.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mysql.cj.jdbc.Driver;

import javax.sql.DataSource;
import java.io.*;
import java.sql.*;
import java.util.Properties;

/**
 * @Author lcw
 * @Description 类的功能描述
 * @Date 2023/4/20
 */
public class JdbcUtils {

    //数据源
    private  static DataSource ds;
    static Properties pro = new Properties();
    static {
       try {


          // InputStream is = new FileInputStream("F:\\workspace2301\\day0420\\src\\mysql.properties");

           //通过类加载器 找到 class所在的根目录
           InputStream is = JdbcUtils.class.getResourceAsStream("/mysql.properties");

           InputStreamReader isr = new InputStreamReader(is,"UTF-8");


           pro.load(isr);

           ds = DruidDataSourceFactory.createDataSource(pro);

       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
    //副本  每个线程有自己独立的变量 ，  线程安全问题
    // set 方法设置 get获取
  static ThreadLocal<Connection>  threadLocal = new ThreadLocal<>();


    //获取连接
    public  static Connection  getConn() {
        try {

            if(threadLocal.get() == null) {

                Connection conn = ds.getConnection();

                //把连接放入ThreadLocal
                threadLocal.set(conn);

            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return  threadLocal.get();
    }



    //关闭连接
    public  static void  close(ResultSet rs , Statement st, Connection
                                conn) {

        try {
            if( rs!= null) {
                rs.close();
            }

            if( st!= null) {
                st.close();
            }

            if( conn!= null) {
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

    //开启事务
    public static Connection  begin() {

        try {
            //1.获取连接
            Connection conn = getConn();
            //2.设置手动提交方式
            conn.setAutoCommit(false);// true 自动 false 手动

            return  conn;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return  null;
    }


    //提交事务
    public static  void  commit() {
        try {
            Connection conn = getConn();
            conn.commit();// 提交   ==>  内存中 持久化磁盘
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    //回滚事务
    public static void  rollback() {
        try {
            Connection conn = getConn();
            conn.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    //返回DataSoucre
    public static DataSource   getDs() {
        return  ds;
    }


}
